package com.zyh.iterator;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @Author zyh
 * @Date 2024/8/1 8:57
 * @Description 迭代器对象
 * 迭代器遍历
 * --java.util.NoSuchElementException
 * --java.util.ConcurrentModificationException 并发修改异常
 */

public class IteratorDemo1 {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(3);
        list.add(5);
        list.add(4);
        list.add(2);
        list.add(1);
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            Integer num = iterator.next();
            if (num == 3 || num == 1) {
                iterator.remove();
            } else {
                System.out.println("num = " + num);
            }
        }
        System.out.println("list = " + list);
    }
}
